package com.facebook.frescodemo;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class MainActivity extends ActionBarActivity {
    boolean status;
    SimpleDraweeView simpleDraweeView, net_image_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleDraweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
        net_image_view = (SimpleDraweeView) findViewById(R.id.net_image_view);
        /**一，加载资源文件中的GIF**/
        Uri uri = Uri.parse("res://" + getPackageName() + "/" + getResources().getIdentifier("dong", "mipmap", getPackageName()));

        //设置图片请求
        ImageRequest request = ImageRequestBuilder
                .newBuilderWithSource(uri)
                .build();

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                        //设置自动播放
                .setAutoPlayAnimations(true)
                .build();
        simpleDraweeView.setController(controller);

        /**二，加载网络上的的GIF**/

        Uri net_uir = Uri.parse("http://pic11.nipic.com/20101107/5963073_142624035351_2.gif");
        ImageRequest net_request = ImageRequestBuilder
                .newBuilderWithSource(net_uir)
                .build();

        DraweeController net_controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(net_request)
                        //设置自动播放
                .setAutoPlayAnimations(true)
                .build();
        net_image_view.setController(net_controller);


    }


    //设置监听器，用来控制gif动画
    public void click(View view) {
        switch (view.getId()) {
            case R.id.button1:

                Animatable animation = simpleDraweeView.getController().getAnimatable();

                if (animation != null) {

                    if (status) {
                        animation.stop();
                        status = false;
                    } else {
                        animation.start();
                        status = true;
                    }

                }


                break;


        }

    }
}
